package com.zl.learning.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.text.ParseException;

/**
 * Created by zhaolei on 2017/6/19.
 */
public class HelloScheduler {
    public static void main(String[] args) throws SchedulerException, ParseException, InterruptedException {
        //创建一个Detail实例,将该实例与HelloJob Class 绑定
        JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob", "group1")
//                .usingJobData("name","This is jobDetailData")
                .build();
        System.out.println(jobDetail.getKey().getName()+":"+jobDetail.getKey().getGroup()+":"+jobDetail.getKey().getClass()+":"+jobDetail.getDescription());
        //创建一个Tigger实例，定义该job立即执行，并且每隔两秒钟重复执行一次，直到永远
//        Trigger trigger = TriggerBuilder.newTrigger()
//                .withIdentity("myTrigger", "group1")
//                .usingJobData("name","这是triggerData")
//                .startNow()
//                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever())
//                .build();
        CronTrigger cronTrigger = TriggerBuilder.newTrigger()
                .withIdentity("cronTrigger","group2")
                .withSchedule(CronScheduleBuilder.cronSchedule("* * * * * ? *"))
                .build();
        //创建Scheduler实例
        SchedulerFactory sfact = new StdSchedulerFactory();
        Scheduler scheduler = sfact.getScheduler();
        scheduler.start();
        scheduler.scheduleJob(jobDetail,cronTrigger);

//        Thread.sleep(1000);
//        scheduler.pauseJob(jobDetail.getKey());
//        scheduler.unscheduleJob(cronTrigger.getKey());
//        scheduler.deleteJob(jobDetail.getKey());
//
//        Date date = new Date();
//        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        System.out.println("current Exce Time is:"+sf.format(date));
//        System.out.println("HelloWorld!");
    }
}
